package com.xzlcPT.controller;

import com.util.PageBean;
import com.xzlcPT.bean.XzLogin;
import com.xzlcPT.bean.XzMember;
import com.xzlcPT.service.LoginUserService;
import com.xzlcPT.service.XzMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/13.
 */

@Controller
@RequestMapping("Member")
@SessionAttributes("userLogin")
public class MemberController extends BaseController{
    @Autowired
    private XzMemberService memberService;
    @Autowired
    private LoginUserService loginUserService;

    @InitBinder
    public void initBinder(WebDataBinder binder){
        //日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat, true));
    }

    @ResponseBody
    @RequestMapping("updateMember.do")
    public Map updateMember(@RequestBody XzMember member){
        Map<String,Object> map = new HashMap();
        int i = memberService.updateMember(member);
        if (i==1){
            map.put("msg","ok");
        }else{
            map.put("msg","err");
        }
        return map;
    }

    //按个人id查询个人详情
    @RequestMapping("selMemberInfo.do")
    public ModelAndView selMemberInfo(@ModelAttribute("userLogin") XzLogin userLogin){
        ModelAndView mv = new ModelAndView("foreEnd3/personalinformation");
        System.out.println(userLogin.getMember().getMemberId());
        XzMember xzMember = memberService.selXzMemberInf(userLogin.getMember().getMemberId());
        System.out.println(xzMember);
        mv.addObject("member",xzMember);
        return mv;
    }

    @RequestMapping("updateMemberInfo.do")
    public ModelAndView updateMemberInfo(@ModelAttribute("userLogin") XzLogin xzLogin, XzMember member){
        ModelAndView mv = new ModelAndView("foreEnd3/index");
        Map<String,Object> map = new HashMap();
        member.setLoginId(xzLogin.getLoginId());
        int i = memberService.updateByPrimaryKeySelective(member);
        if (i==1){
            map.put("msg","ok");
        }else{
            map.put("msg","err");
        }
        XzMember xzMember = memberService.selXzMemberInf(member.getMemberId());
        mv.addObject("map",map);
        mv.addObject("member",xzMember);
        return mv;
    }

    //后台管理员操作方法-------------------------------------------------------------------
    //后台首页查找所有member
    @RequestMapping("selAllMem.emp")
    public ModelAndView selAllMem(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "10")int rows,XzMember xzMember){
        ModelAndView mv = new ModelAndView("backEnd3/member_index");
        PageBean<XzMember> pageBean = memberService.selAllMem(page,rows,xzMember);
        mv.addObject("member",xzMember);
        mv.addObject("list",pageBean.getList());
        mv.addObject("page",pageBean.getPageNum());
        mv.addObject("pages",pageBean.getPages());
        mv.addObject("total",pageBean.getTotal());
        System.out.println(xzMember);
        return mv;
    }

    //后台按memberId查找
    @RequestMapping("selByMenberIdEmp.emp")
    public ModelAndView selByMenberIdEmp(Long memberId,int a){
        ModelAndView mv = new ModelAndView("backEnd3/member_revise");
        XzMember xzMember = memberService.selByMenberIdEmp(memberId);
        if(a==2){
            mv.setViewName("backEnd3/member_updata");
        }
        mv.addObject("member",xzMember);
        return mv;
    }
    //后台管理员修改
    @RequestMapping("updateByEmp.emp")
    public ModelAndView updateByEmp(XzMember member){
        ModelAndView mv = new ModelAndView("redirect:selAllMem.emp");
        Map map = new HashMap();
        int i = memberService.updateByEmp(member);
        if(i==1){
            map.put("msg","ok");
        }else {
            map.put("msg","err");
        }
        mv.addObject("map",map);
     return mv;
    }

    //移动端异步------------------------------------------------------------------
    //按个人id查询个人详情
    @ResponseBody
    @RequestMapping("selMemberInfoMob.go")
    public Map selMemberInfoMob(@RequestHeader("accessToken") String token){
        XzLogin xzLogin = loginUserService.selByToken(token);
        Map map = new HashMap();
        XzMember xzMember = memberService.selXzMemberInf(xzLogin.getMember().getMemberId());
        System.out.println(xzMember);
        map.put("member",xzMember);
        return map;
    }

    //修改个人信息
    @ResponseBody
    @RequestMapping("updateMemberInfoMob.go")
    public Map updateMemberInfoMob(XzMember member){
        Map map = new HashMap();
        int i = memberService.updateByPrimaryKeySelective(member);
        if (i==1){
            map.put("msg","ok");
        }else{
            map.put("msg","err");
        }
        XzMember xzMember = memberService.selXzMemberInf(member.getMemberId());
        map.put("member",xzMember);
        return map;
    }
}
